package cn.hwua.dao.impl;

import cn.hwua.dao.AdminDao;
import cn.hwua.entity.Admin;
import cn.hwua.util.DBUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class AdminDaoImpl implements AdminDao {
    Connection conn = DBUtil.getConnection();
    QueryRunner qr = new QueryRunner();

    @Override
    public int save(Admin obj) {
        String sql = "INSERT INTO  tb_admin(admin_id,admin_username,admin_pwd,admin_email,admin_phone,admin_nickname) VALUES (0,?,?,?,?,?)";
        Object para[] = {obj.getAdmin_username(), obj.getAdmin_pwd(), obj.getAdmin_email(), obj.getAdmin_phone()
                , obj.getAdmin_nickname()};
        Connection conn = DBUtil.getConnection();
        QueryRunner runner = new QueryRunner();
        try {
            int row = runner.update(conn, sql, para);
            return row;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public int update(Admin obj) {
        String sql = "UPDATE  tb_admin SET  admin_username=?,admin_pwd=? WHERE admin_id=? ";
        Object[] paras = {obj.getAdmin_username(), obj.getAdmin_pwd(), obj.getAdmin_id()};
        try {
            int row = qr.update(conn, sql, paras);
            return row;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public int delete(Admin obj) {
        String sql = "DELETE FROM tb_admin WHERE admin_id  = ? ";
        Object[] paras = {obj.getAdmin_id()};
        try {
            int row = qr.update(conn, sql, paras);
            return row;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public Admin findById(Object id) {
        String sql = "SELECT * FROM tb_admin where admin_id  = ?";
        Object[] paras = {id};
        try {
            Admin admin = qr.query(conn, sql, paras, new BeanHandler<Admin>(Admin.class));
            return admin;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<Admin> findAll() {
        String sql = "SELECT * FROM tb_admin";
        Object[] paras = {};
        try {
            List<Admin> adminList = qr.query(conn, sql, paras, new BeanListHandler<Admin>(Admin.class));
            return adminList;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<Admin> findSearch(String keyword) {
        String sql = "SELECT * FROM tb_admin where admin_username like concat('%',concat(?,'%'))";
        Object[] paras = {keyword};
        try {
            List<Admin> adminList = qr.query(conn, sql, paras, new BeanListHandler<Admin>(Admin.class));
            return adminList;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<Admin> findPage(int page, int pagesize) {
        String sql = "SELECT * FROM tb_admin limit ?,?";
        Object[] paras = {(page - 1) * pagesize, pagesize};
        try {
            List<Admin> adminList = qr.query(conn, sql, paras, new BeanListHandler<Admin>(Admin.class));
            return adminList;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public long count() {
        String sql = "select count(*) from tb_admin";
        Object[] paras = {};
        try {
            long nums = qr.query(conn, sql, paras, new ScalarHandler<Long>());
            return nums;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public Admin login(String name) {
        String sql = "SELECT * FROM tb_admin where admin_username = ? or admin_email=? or admin_phone=? or admin_nickname=?";
        Object[] paras = {name,name,name,name};
        try {
            Admin admin = qr.query(conn, sql, paras, new BeanHandler<Admin>(Admin.class));
            return admin;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public boolean check(String key, String val) {
        String sql="SELECT * FROM tb_admin where "+key+" =? ";
        Object para[] = {val};//参数
        try {
            Admin admin = qr.query(conn, sql, para, new BeanHandler<Admin>(Admin.class));
            return null!=admin;
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return false;
    }
}
